package org.jsola.hr.constant.convert;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.springframework.util.StringUtils;

/**
 * @author lyh
 */
public class BooleanConverter implements Converter<Boolean> {

    private final String YES = "是";
    private final String NO = "否";

    @Override
    public Class<Boolean> supportJavaTypeKey() {
        return Boolean.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.BOOLEAN;
    }

    @Override
    public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        if (YES.equals(cellData.getStringValue())) {
            return true;
        } else if (StringUtils.isEmpty(cellData.getStringValue()) || NO.equals(cellData.getStringValue())) {
            return false;
        } else {
            return false;
        }
    }

    @Override
    public CellData<Boolean> convertToExcelData(Boolean value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        return new CellData<>(value ? YES : NO);
    }
}
